package agents.util;

import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;

public class ContractHistory<K, V> {
	
	private HashMap<K, HashMap<String, TreeSet<V>>> log;
	
	public ContractHistory(){
		log = new HashMap<K, HashMap<String,TreeSet<V>>>();
	}
	
	public V put(K key, String entry, V evt){
		if(log.containsKey(key)){
			HashMap<String, TreeSet<V>> entryEvents = log.get(key);
			if(entryEvents.containsKey(entry)){
				TreeSet<V> events = entryEvents.get(entry);
				events.add(evt);
			}
			else{
				TreeSet<V> events = new TreeSet<V>();
				events.add(evt);
				entryEvents.put(entry, events);
			}
		}
		else{
			HashMap<String, TreeSet<V>> entryEvents = new HashMap<String, TreeSet<V>>();
			TreeSet<V> events = new TreeSet<V>();
			events.add(evt);
			entryEvents.put(entry, events);
			log.put(key, entryEvents);
		}
		return evt;
	}
	
	public Set<V> getHistory(K key, String entry){
		if(log.containsKey(key))
			if(log.get(key).containsKey(entry))
				return log.get(key).get(entry);
			else return null;
		else return null;
	}
	
	public Set<K> keySet(){
		return log.keySet();
	}
	
	public Set<String> keyEntrySet(K key){
		return log.get(key).keySet();
	}
}
